package com.property.manage.service.equipment;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guardchina.framework.mybatis.model.CustomPage;
import com.guardchina.framework.util.exception.BusinessException;
import com.guardchina.framework.util.utils.DateUtil;
import com.guardchina.framework.util.utils.PhoneUtil;
import com.property.manage.dao.equipment.MaintenanceMapper;
import com.property.manage.model.equipment.MaintenanceEntity;
import com.property.manage.model.equipment.MaintenanceVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Component
@Slf4j
public class MaintenanceService {
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private MaintenanceMapper maintenanceMapper;



    public MaintenanceVo findParking(Integer id) {
        return maintenanceMapper.queryById(id);
    }

    public CustomPage<MaintenanceVo> page(Long pageNo, Long pageSize, Integer villageId,
                                      String name) {
        Page<MaintenanceVo> page = new Page<>(pageNo, pageSize);
        IPage<MaintenanceVo> village = maintenanceMapper.getPage(page,villageId,name);
        return new CustomPage<>(village);
    }

    @Transactional(rollbackFor = Exception.class)
    public void add(String name, String number, LocalDate maintenanceTime, Integer villageId, LocalDate nextTime,
                    BigDecimal money,
                    String personnel, String phone
    ) {
        int eqId = equipmentService.querEqId(name, number, villageId);
        if (!PhoneUtil.isMobile(phone)){
            throw new BusinessException("请填写正确的手机号码");
        }
        MaintenanceEntity entity = new MaintenanceEntity();
        entity.setEquipmentId(eqId);
        entity.setMaintenanceTime(maintenanceTime);
        entity.setMoney(money);
        entity.setNextTime(nextTime);
        entity.setPersonnel(personnel);
        entity.setPhone(phone);
        try{
            maintenanceMapper.insert(entity);
            if (nextTime!=null&&!StringUtils.isBlank(DateUtil.fromLocalDate(nextTime))){
                equipmentService.updateNextTime(eqId,DateUtil.fromLocalDate(nextTime));
            }
            if (nextTime==null){
                equipmentService.updateNextTime(eqId,null);
            }
        }catch (Exception e){
            throw new BusinessException("新增记录失败",String.format("新增设备保养与维修记录失败[%s]",e));
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public void update(Integer id,String name, String number, LocalDate maintenanceTime, Integer villageId, LocalDate nextTime,
                       BigDecimal money,
                       String personnel, String phone) {
        if (!PhoneUtil.isMobile(phone)){
            throw new BusinessException("请填写正确的手机号码");
        }
        int eqId = equipmentService.querEqId(name, number, villageId);
        UpdateWrapper<MaintenanceEntity> wrapper = new UpdateWrapper<>();
        wrapper.set("equipment_id", eqId);
        wrapper.set("maintenance_time",maintenanceTime);
        wrapper.set("next_time", nextTime);
        wrapper.set("money", money);
        wrapper.set("personnel", personnel);
        wrapper.set("phone",phone);
        wrapper.eq("id", id);
        try{
            maintenanceMapper.update(null, wrapper);
            if (nextTime!=null&&!StringUtils.isBlank(DateUtil.fromLocalDate(nextTime))){
                equipmentService.updateNextTime(eqId,DateUtil.fromLocalDate(nextTime));
            }
            if (nextTime==null){
                equipmentService.updateNextTime(eqId,null);
            }
        }catch (Exception e){
            throw new BusinessException("修改记录失败",String.format("修改设备保养与维修记录失败[%s]",e));
        }
    }


    public void delete(Integer id) {
        maintenanceMapper.deleteById(id);
    }


}
